﻿#include "UserInfoCodec.h"

#include "TcpCodec.h"
#include "DeviceInfoManager.h"

UserInfoCodec::UserInfoCodec()
{

}

UserInfoCodec::~UserInfoCodec()
{

}

void UserInfoCodec::getAllDeviceFromServer()
{
  DataPackage_TcpCodec dataPackage_TcpCodec;
  tcpCodec->code((&dataPackage_TcpCodec)\
                 ->setPackageType(TcpCodec::User)
                 ->setData(QByteArray(1,AllDevice)));
}

void UserInfoCodec::decode(QByteArray data)
{

  if(AllDevice==data[0])
    {
      QVector<Device> deviceList;
      bool isOk=false;
      deviceList=Device::fromByteArrays(data.mid(1),&isOk);
      if(isOk)
        {
          deviceInfoManager->setDeviceInfoList(&deviceList);
        }
    }
}

TcpCodec* UserInfoCodec::getTcpCodec()const
{
  return tcpCodec;
}

UserInfoCodec* UserInfoCodec::setTcpCodec(TcpCodec* const tcpCodec)
{
  this->tcpCodec=tcpCodec;
  printInjection(this,tcpCodec);
  return this;
}

DeviceInfoManager* UserInfoCodec::getDeviceInfoManager()const
{
  return deviceInfoManager;
}

UserInfoCodec* UserInfoCodec::setDeviceInfoManager(DeviceInfoManager* const deviceInfoManager)
{
  this->deviceInfoManager=deviceInfoManager;
  printInjection(this,deviceInfoManager);
  return this;
}

void UserInfoCodec::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setTcpCodec(getObjFromMap<TcpCodec>(objMap));
  setDeviceInfoManager(getObjFromMap<DeviceInfoManager>(objMap));
}
